package com.pmcc.cj.service;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.pmcc.cj.entity.CjAreaAgency;
import com.pmcc.cj.entity.FjCity;
import com.pmcc.cj.repository.FjCityRepository;
import com.pmcc.utils.*;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class FjCityService {


    @Autowired
    FjCityRepository repository;

    @Autowired
    CjAreaAgencyService service;
    public List<FjCity> query(FjCity model){


            List<FjCity> list=repository.findAll(this.getSpecification(model));


        return list;
    }
    public Specification<FjCity> getSpecification(FjCity model) {
        return new Specification<FjCity>() {
            @Override
            public Predicate toPredicate(Root<FjCity> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(model.getId().equals("省")){
                    list.add(builder.like(root.get("id").as(String.class),  "100"+"%"+"0000"));
                }
                if(model.getId().equals("市")){
                    list.add(builder.like(root.get("id").as(String.class),  model.getName()+"%"+"00"));
                }
                if(model.getId().equals("县")){
                    list.add(builder.like(root.get("id").as(String.class),  model.getName()+"%"));
                }
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }
    public void initArea(){
        FjCity city=new FjCity();
        city.setId("省");
        List<FjCity>list=query(city);
        for(FjCity fc:list){
            if(!fc.getId().equals("100000000")){
                CjAreaAgency caa=new CjAreaAgency();
                caa.setAddress(fc.getName());
                caa.setCode(fc.getId());
                caa.setLevel(1);
                service.addAreaAgency(caa);
                String arr=caa.getCode().substring(0,5);
                city.setId("市");
                city.setName(arr);
                List<FjCity>list1=query(city);
                String noarr=arr+"0000";
                for(FjCity fc1:list1){
                    if(!fc1.getId().equals(noarr)){
                        CjAreaAgency c1=new CjAreaAgency();
                        c1.setAddress(fc1.getName());
                        c1.setCode(fc1.getId());
                        c1.setParentId(caa.getId());
                        c1.setLevel(2);
                        service.addAreaAgency(c1);
                        city.setId("县");
                        arr=c1.getCode().substring(0,7);
                        city.setName(arr);
                        List<FjCity>list2=query(city);
                        for(FjCity fc2:list2) {
                            if(!fc2.getId().equals(arr+"00")){
                                CjAreaAgency c2 = new CjAreaAgency();
                                c2.setAddress(fc2.getName());
                                c2.setCode(fc2.getId());
                                c2.setParentId(c1.getId());
                                c2.setLevel(3);
                                service.addAreaAgency(c2);
                            }

                        }
                    }
                }

            }
        }
    }

}
